package cn.gailvlun.gll.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.config.Constants;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.StringUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;



public class CaptchaTextView extends AppCompatTextView {

    private int countDownTime;
    private boolean isInCount;
    private String countingTemplate;

    private boolean startedBefore = false;

    public CaptchaTextView(Context context) {
        this(context, null);
    }

    public CaptchaTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CaptchaTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(attrs);
    }

    private void init(@Nullable AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CaptchaTextView);
        countDownTime = ta.getInt(R.styleable.CaptchaTextView_countDownTime, Constants.Register.smsDuration);
        countingTemplate = ta.getString(R.styleable.CaptchaTextView_countingTemplate);
        if (countingTemplate == null) {
            countingTemplate = getContext().getString(R.string.captcha_counting_template);
        }
        ta.recycle();
    }

    
    public void setLeftTime(int leftTime) {
        if (leftTime <= countDownTime) {
            startCount(leftTime);
        }
    }

    
    public void setCountDownTime(int countDownTime) {
        this.countDownTime = countDownTime;
    }

    public int getCountDownTime() {
        return this.countDownTime;
    }

    
    public void setCountingTemplate(@NonNull String str) {
        countingTemplate = str;
    }

    public String getCountingTemplate() {
        return countingTemplate;
    }

    
    public void startCount() {
        startCount(countDownTime);
    }

    public void stopCount() {
        isInCount = false;
        if (countDownDisposable != null && !countDownDisposable.isDisposed()) {
            countDownDisposable.dispose();
        }
        countDownDisposable = null;
        setEnabled(true);
    }

    private Disposable countDownDisposable;

    
    private void startCount(int leftTime) {
        startedBefore = true;
        setEnabled(false);
        isInCount = true;
        RxUtil.countDown(leftTime)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        countDownDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        setText(StringUtil.format(countingTemplate, aLong));
                    }

                    @Override
                    public void onError(Throwable e) {
                        isInCount = false;
                        setEnabled(true);
                    }

                    @Override
                    public void onComplete() {
                        isInCount = false;
                        setEnabled(true);
                    }
                });
    }

    @Override
    public void setEnabled(boolean enabled) {
        
        if (isInCount) {
            return;
        }
        if (enabled) {
            if (startedBefore) {
                setText("重新发送");
            } else {
                setText("获取验证码");
            }
        }
        super.setEnabled(enabled);
    }
}
